Skip to content

Conversation

@heyseth
Copy link

@heyseth heyseth commented Oct 5, 2025

Related GitHub Issue

Closes: #8239

Description

This PR improves the user experience of importing custom modes by automatically switching to the newly imported mode upon a successful import. This provides immediate feedback to the user and removes the extra step of manually locating and selecting the mode from the list.

The implementation involves a small change to the import process:

  • The backend importModeWithRules() function in CustomModesManager.ts now returns the slug of the successfully imported mode.
  • This slug is passed from the message handler to the ModesView.tsx component in the UI.
  • The UI then uses this slug to programmatically set the active mode, providing an instantaneous update for the user.
  • The logic gracefully handles cases where the slug isn't provided or the import fails, ensuring the current mode remains unchanged.

Test Procedure

Automated Tests

A new test suite, ModesView.import-switch.spec.tsx, has been added to cover the new auto-switch functionality. These tests verify that the UI switches correctly on a successful import with a slug and does not switch on a failed import or if the slug is missing.

Manual Testing Steps

  1. Navigate to the Custom Modes settings page.
  2. Click the "Import Mode" button.
  3. Select a valid custom mode configuration file to import.
  4. Verification: Upon successful import, confirm that the UI automatically switches to and highlights the newly imported mode in the list.
  5. Attempt to import an invalid or corrupted mode file.
  6. Verification: Confirm that an error is shown and that the currently active mode does not change.
  7. These steps should be tested for both global and project-level imports.

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: New and/or updated tests have been added to cover my changes (if applicable).
  • Documentation Impact: I have considered if my changes require documentation updates (see "Documentation Updates" section below).
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Documentation Updates

  • No documentation updates are required.

Additional Notes

The message handler in webviewMessageHandler.ts was updated to be backward compatible. If it receives a result from an older backend version without a slug, it will handle it gracefully without causing errors.


Get in Touch

Discord: @ocean.smith


Important

Automatically switch to a newly imported mode upon successful import, with backend and frontend updates to handle mode slug.

  • Behavior:
    • importModeWithRules() in CustomModesManager.ts now returns the slug of the imported mode.
    • webviewMessageHandler.ts updated to pass slug to UI on successful import.
    • ModesView.tsx uses slug to switch to the imported mode automatically.
    • Handles cases where slug is missing or import fails, keeping the current mode unchanged.
  • Tests:
    • Added ModesView.import-switch.spec.tsx to test auto-switch functionality on successful import.
    • Tests ensure UI switches on success with slug and remains unchanged on failure or missing slug.

This description was created by Ellipsis for a2792d1. You can customize this summary. It will automatically update as commits are pushed.

Fixes [RooCodeInc#8239](RooCodeInc#8239)

## Summary
Automatically switches to the imported mode after a successful mode import,
providing immediate feedback and eliminating the need for manual mode selection.

## Changes

### Backend (CustomModesManager.ts)
- Modified `importModeWithRules()` to return the imported mode's slug in the result
- Returns `{ success: true, slug: importData.customModes[0]?.slug }` on success
- Enables the UI to know which mode was imported for automatic switching

### Message Handler (webviewMessageHandler.ts)
- Updated `importMode` case to handle the new slug return value
- Passes the slug to the webview via `importModeResult` message
- Maintains backward compatibility with existing error handling

### UI (ModesView.tsx)
- Added auto-switch logic in `importModeResult` message handler
- Attempts to find imported mode in fresh customModes list
- Falls back to direct slug-based switch if mode not yet in list
- Updates visual mode state for immediate UI feedback
- Only switches on success with a valid slug provided

### Tests (ModesView.import-switch.spec.tsx)
- Added new test suite for import auto-switch behavior
- Tests successful switch when slug is provided
- Tests no-switch behavior on import failure or missing slug
- Verifies both backend message and UI state updates

## Testing
- New test coverage added for auto-switch scenarios
- Existing import/export functionality remains unchanged
- Works with both global and project-level imports
@heyseth heyseth requested review from cte, jr and mrubens as code owners October 5, 2025 21:12
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request UI/UX UI/UX related or focused labels Oct 5, 2025
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Oct 5, 2025
Copy link

@roomote roomote bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found an issue that should be addressed before merge.

…tener

Use a stable ref to hold the latest switchMode and avoid capturing it in the window "message" listener closure:
Add a switchModeRef updated via useEffect; call switchModeRef.current in the importModeResult fallback instead of switchMode
Mirrors existing handleModeSwitchRef/customModesRef pattern to keep handler stable while accessing fresh values
Prevents re-registration churn and removes the ESLint warning with no runtime behavior change

File touched:
webview-ui/src/components/modes/ModesView.tsx

Why:
The effect that registers the window event listener intentionally has an empty dependency array; directly referencing switchMode violates react-hooks/exhaustive-deps. Using a ref decouples the effect from function identity changes while preserving up-to-date behavior.
Copy link

@roomote roomote bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found some issues that need attention. See inline comments for details.


return { success: true }
// Return the imported mode's slug so the UI can activate it
return { success: true, slug: importData.customModes[0]?.slug }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Multi-mode import behavior: this returns only the first imported mode's slug (customModes[0]). If a YAML contains multiple modes, the UI will switch to the first only. Either (a) validate single-mode imports and surface a clear error, or (b) return a list of slugs (or the last processed slug) and adapt the UI to a defined behavior.

Copy link
Author

@heyseth heyseth Oct 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What behavior do we want from Roo when the user imports a modes.yaml file containing multiple modes? Personally I think auto-selecting the first mode from the list is fine (this is the current behavior in my implementation).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thoughts? @hannesrudolph

Type safety for slug

Co-authored-by: roomote[bot] <219738659+roomote[bot]@users.noreply.github.com>
Copy link

@roomote roomote bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a new issue not covered by existing comments. See inline comment for details.

…rt success

updateCustomMode() was catching and swallowing all errors, causing
importModeWithRules() to return success even when mode persistence failed.
This led to auto-switch attempting to activate modes that never persisted.
…rt' of https://github.com/heyseth/Roo-Code into feat/auto-switch-to-imported-mode-after-successful-import
Copy link

@roomote roomote bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found some issues that need attention. See inline comments for details.

Moves type declaration out of event handler to prevent redeclaration on every message event.
…ejections while allowing importModeWithRules to detect persistence failures
Copy link

@roomote roomote bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found some issues that need attention. See inline comments for details.

…issues

- Use parseYamlSafely() in importModeWithRules for consistent YAML parsing
across the codebase, ensuring BOM stripping and character cleaning
- Sync visualMode with context.mode to prevent UI desync when modes are
switched programmatically from outside the component
- Add test coverage for fallback branch when imported mode slug is not
yet present in customModes state

Fixes parser inconsistency (P2), visualMode desync risk (P3), and missing
test coverage (P3) identified in code review.
Copy link

@roomote roomote bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found some issues that need attention. See inline comments for details.

Copy link

@roomote roomote bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found some issues that need attention. See inline comments for details.

@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Oct 28, 2025
@hannesrudolph hannesrudolph added PR - Needs Preliminary Review and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Oct 28, 2025
daniel-lxs added a commit that referenced this pull request Nov 3, 2025
Based on PR #8521 by @heyseth (Seth Miller) with the following enhancements:

Original work by Seth Miller:
- Auto-switch to imported mode after successful import
- Backend returns imported mode slug in importModeWithRules()
- UI switches to imported mode when found in customModes list
- Comprehensive test coverage for auto-switch functionality

Additional improvements:
- Use architect mode as fallback when imported slug not yet present
- Updated test to expect architect mode in fallback scenario
- Prevents UI desync during state refresh race conditions

Co-authored-by: Seth Miller <[email protected]>
@daniel-lxs
Copy link
Member

This PR has been superseded by #9003 which incorporates your auto-switch functionality with an additional enhancement for the fallback behavior.

#9003 includes:

  • All original work (auto-switch, backend slug return, UI switching, tests)
  • Enhanced fallback to architect mode when imported slug not yet present
  • Full attribution with Co-authored-by credit

Thank you for the solid foundation.

@daniel-lxs daniel-lxs closed this Nov 3, 2025
@github-project-automation github-project-automation bot moved this from PR [Needs Prelim Review] to Done in Roo Code Roadmap Nov 3, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Nov 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request PR - Needs Preliminary Review size:L This PR changes 100-499 lines, ignoring generated files. UI/UX UI/UX related or focused

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[ENHANCEMENT] Auto-switch to imported mode after import (reduces friction)

3 participants